<?php

class Course_Choose {
    
    const TABLE_NAME = 'course';
    /**
     * db实例
     *
     * @var Helper_Cache_Memcache
     */
    protected $_dbInstance;
    /**
     * 单例对象
     */
    public static $singletonObjects;

    /**
     * 获取单例
     */
    public static function getInstance() {
        $className = __CLASS__;
        if (!isset(self::$singletonObjects[$className])) {
            self::$singletonObjects[$className] = new self();
        }

        return self::$singletonObjects[$className];
    }

    /**
     * 构造器
     *
     * @param int $uId
     */
    protected function __construct() {

        $this->_dbInstance = $this->_getMysql();
//        $this->_cacheInstance = $this->_getRedis();
    }

    /*
     * 获取cache实例
     */

    public function _getCache() {

        return Common::getCache();
    }

    /*
     * 获取redis实例
     */

    public function _getRedis() {
//        $config = array ('current'=>'127.0.0.1' , 'old' =>'6380') ;
//        $redis = new Helper_Redis($config);
        $redis = new Redis();
        $redis->connect('127.0.0.1', 6380);
        return $redis;
    }

    /*
     * 获取db实例
     */

    public function _getMysql() {

        return Common::getMysql();
    }
    
    /*
     * fid=0
     * 获取课程大类
     */
    public function getCourseList (  )  {
        
        return $this->_dbInstance->getList( self::TABLE_NAME,array( 'fid'=>'0' ) );
        
    }
    /**
     *  根据课程父id获得子id
     * @param type $fid 
     */
    public function getChildCourseByFId( $fid ){
        $courseInfo = Common::getmysql()->getList( self::TABLE_NAME , array( 'fid'=> $fid ) , array( 'id' => 'desc'));
        return $courseInfo;
    }
    /**
     * 查询所有课程
     */
    public function getAllCourse(){
        $fatCourse = Common::getMysql()->getList( self::TABLE_NAME , array( 'fid' => 0));
        foreach( $fatCourse as $key => $value){
          $courseInfo[$value['id']] = array(
            'childCourse' => $this->getChildCourseByFId( $value['id'] ),
            'fatCourse' => $value['name']
                
          ); 
        }
        //print_r( $courseInfo);
        return $courseInfo;
        
    }
    
    /*
     * 根据课程大类name
     * 获取子课程
     */
    public function getSonCourse ( $courseName ) {
        $fatherCourse = $this->_dbInstance->get(self::TABLE_NAME, array('name'=>$courseName));
        $fatheCourseId = $fatherCourse['id'];
        return $this->_dbInstance->getList(self::TABLE_NAME, array('fid'=>$fatheCourseId));
    }
}
?>
